package com.example.order.activity;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.order.R;
import com.example.order.utils.Utils;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Main8Activity extends AppCompatActivity implements View.OnClickListener {

    EditText edit1;
    EditText edit2;
    EditText edit3;
    EditText edit4;
    EditText edit5;
    EditText edit6;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main8);
        //注册
        edit1=findViewById(R.id.edit_submit1);
        edit2=findViewById(R.id.edit_submit2);
        edit3=findViewById(R.id.edit_submit3);
        edit4=findViewById(R.id.edit_submit4);
        edit5=findViewById(R.id.edit_submit5);
        edit6=findViewById(R.id.edit_submit6);
        Button btn_submit=findViewById(R.id.btn_submit1);
        Button btn_quit=findViewById(R.id.btn_submit2);
        btn_submit.setOnClickListener(this);
        btn_quit.setOnClickListener(this);


    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onClick(View v) {
        EditText[] editTexts =new EditText[]{
                edit1,edit2,edit3,edit4,edit5,edit6};
        switch (v.getId())
        {

            case R.id.btn_submit1:
                //注册
                boolean flag=true;
                for (EditText editText:editTexts) {
                    if(!Utils.isNotNullString(editText.getText().toString()))
                    {
                        flag=false;
                        Utils.showToast(this,"所有输入框均不能为空",false);
                    }
                }
                //手机号是否合法
                if(flag &&!Utils.isMobile(edit2.getText().toString()))
                {
                    flag=false;
                    Utils.showToast(this,"手机号码必须为中华人民共和国大陆地区11位合法手机号码",false);
                }
                //密码是否相等
                if(flag &&!edit3.getText().toString().equals(edit4.getText().toString()))
                {
                    flag=false;
                    Utils.showToast(this,"两次输入的密码不一致",false);
                }

                if(flag)
                {
                    //提交
                    //Utils.showToast(this,"校验成功",false);
                    HashMap<String,String> map=new HashMap<>();
                    map.put("name",edit1.getText().toString());
                    map.put("phone",edit2.getText().toString());
                    map.put("password",edit4.getText().toString());
                    map.put("flavor",edit5.getText().toString());
                    map.put("taboo",edit6.getText().toString());
                    Utils.sendPostHttpRequest("customer/submit",
                            map,
                            new Callback() {
                                @Override
                                public void onFailure(Call call, IOException e) {
                                    Log.e("Main8Activity",
                                            Arrays.toString(  e.getStackTrace()));
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Utils.showToast(Main8Activity.this,
                                                    "注册失败,手机号可能已经存在"
                                            ,true);
                                        }
                                    });
                                }

                                @Override
                                public void onResponse(Call call, Response response) throws IOException {
                                    String string = response.body().string();
                                    Log.e("Main8Activity",string);
                                    if(string.equals("true"))
                                    {
                                        //注册成功
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Utils.showToast(Main8Activity.this,
                                                        "注册成功",
                                                        false);
                                            }
                                        });
                                    }
                                    else
                                    {
                                        //注册失败
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Utils.showToast(Main8Activity.this,
                                                        "注册失败,请联系管理员",
                                                        true);
                                            }
                                        });

                                    }


                                }
                            });


                }
                break;
            case R.id.btn_submit2:
                //取消
                for (EditText editText:editTexts) {
                    editText.setText("");
                }

                break;
            default:
                break;
        }

    }
}
